package com.sensePlusVR.bean;

import java.util.ArrayList;
import java.util.List;

import org.xutils.DbManager;
import org.xutils.db.annotation.Column;
import org.xutils.db.annotation.Table;
import org.xutils.db.sqlite.WhereBuilder;
import org.xutils.ex.DbException;

import android.content.Context;
import android.os.Parcel;
import android.os.Parcelable;

import com.sensePlusVR.application.VRApplication;
import com.sensePlusVR.utils.Constant;
import com.sensePlusVR.utils.MyLog;

@Table(name = "ActorItem")
public class ActorItem /* extends EntityBase */implements Parcelable {

	private static final String TAG = ActorItem.class.getSimpleName();

	@Column(name = "id", isId = true, autoGen = false)
	private int id;

	@Column(name = "area")
	private String area;

	@Column(name = "createtime")
	private String createtime;

	@Column(name = "summary")
	private String summary;

	@Column(name = "style")
	private String style;

	@Column(name = "company")
	private String company;

	@Column(name = "age")
	private int age;

	@Column(name = "name")
	private String name;

	@Column(name = "followed")
	private int followed;

	@Column(name = "videoid")
	private String videoid;

	@Column(name = "imgurl")
	private String imgurl;

	@Column(name = "userfollowcount")
	private int userfollowcount;

	@Column(name = "bwh")
	private String bwh;

	@Column(name = "weight")
	private String weight;

	@Column(name = "height")
	private String height;

	@Column(name = "birthday")
	private String birthday;

	@Column(name = "hair")
	private String hair;

	@Column(name = "eyes")
	private String eyes;

	@Column(name = "ethnicity")
	private String ethnicity;

	@Column(name = "language")
	private String language;

	public ActorItem() {

	}

	public ActorItem(Parcel in) {
		id = in.readInt();
		area = in.readString();
		createtime = in.readString();
		summary = in.readString();
		style = in.readString();
		company = in.readString();
		age = in.readInt();
		name = in.readString();
		followed = in.readInt();
		videoid = in.readString();
		imgurl = in.readString();
		userfollowcount = in.readInt();
		bwh = in.readString();
		weight = in.readString();
		height = in.readString();
		birthday = in.readString();
		hair = in.readString();
		eyes = in.readString();
		ethnicity = in.readString();
		language = in.readString();

	}

	public String getArea() {
		return area;
	}

	public void setArea(String area) {
		this.area = area;
	}

	public String getCreatetime() {
		return createtime;
	}

	public void setCreatetime(String createtime) {
		this.createtime = createtime;
	}

	public String getSummary() {
		return summary;
	}

	public void setSummary(String summary) {
		this.summary = summary;
	}

	public String getStyle() {
		return style;
	}

	public void setStyle(String style) {
		this.style = style;
	}

	public String getCompany() {
		return company;
	}

	public void setCompany(String company) {
		this.company = company;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getVideoid() {
		return videoid;
	}

	public void setVideoid(String videoid) {
		this.videoid = videoid;
	}

	public String getImgurl() {
		return imgurl;
	}

	public void setImgurl(String imgurl) {
		this.imgurl = imgurl;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getFollowed() {
		return followed;
	}

	public void setFollowed(int followed) {
		this.followed = followed;
	}

	public int getUserfollowcount() {
		return userfollowcount;
	}

	public void setUserfollowcount(int userfollowcount) {
		this.userfollowcount = userfollowcount;
	}
	
	public String getBwh() {
		return bwh;
	}

	public void setBwh(String bwh) {
		this.bwh = bwh;
	}

	public String getWeight() {
		return weight;
	}

	public void setWeight(String weight) {
		this.weight = weight;
	}

	public String getHeight() {
		return height;
	}

	public void setHeight(String height) {
		this.height = height;
	}

	public String getBirthday() {
		return birthday;
	}

	public void setBirthday(String birthday) {
		this.birthday = birthday;
	}

	public String getHair() {
		return hair;
	}

	public void setHair(String hair) {
		this.hair = hair;
	}

	public String getEyes() {
		return eyes;
	}

	public void setEyes(String eyes) {
		this.eyes = eyes;
	}

	public String getEthnicity() {
		return ethnicity;
	}

	public void setEthnicity(String ethnicity) {
		this.ethnicity = ethnicity;
	}

	public String getLanguage() {
		return language;
	}

	public void setLanguage(String language) {
		this.language = language;
	}

	public static boolean isRealActor(ActorItem actorItem) {
		boolean isRealActor = false;
		String styleStr = actorItem.getStyle();
		String[] styles = styleStr.split(":");
		if (styles.length > 1) {
			isRealActor = styles[1].equals(Constant.TYPE_REAL);
			MyLog.d(TAG, "styles[1]==" + styles[1] + " isRealActor : "
					+ isRealActor);
		}
		return isRealActor;
	}

	public static void saveOrUpdate(Object object) {
		try {
			Context context = VRApplication.getAppContext();
			if (context != null) {
				DbManager db = VRApplication.getDbManager();
				db.saveOrUpdate(object);
			}
		} catch (Exception e) {
			MyLog.e(TAG, "ActorItem update failed: " + e);
		}
	}

	public static ActorItem getActorByName(String actorName) {
		Context context = VRApplication.getAppContext();
		if (context != null) {
			DbManager db = VRApplication.getDbManager();
			try {
				return db.selector(ActorItem.class)
						.where("name", "=", actorName).findFirst();
			} catch (DbException e) {
				MyLog.e(TAG, "getActorByName findFirst failed: " + e);
				return null;
			}
		}
		return null;
	}

	public static ActorItem getActorById(int actorId) {
		Context context = VRApplication.getAppContext();
		if (context != null) {
			DbManager db = VRApplication.getDbManager();
			try {
				return db.selector(ActorItem.class).where("id", "=", actorId)
						.findFirst();
			} catch (DbException e) {
				MyLog.e(TAG, "getActorByName findFirst failed: " + e);
				return null;
			}
		}
		return null;
	}

	public static List<ActorItem> getAllActors(int limit, int offset) {
		Context context = VRApplication.getAppContext();
		if (context != null) {
			DbManager db = VRApplication.getDbManager();
			try {
				return db.selector(ActorItem.class).orderBy("createtime", true)
						.limit(limit).offset(offset).findAll();
			} catch (DbException e) {
				MyLog.e(TAG, "getGameListSort failed: " + e);
				return null;
			}
		}
		return null;
	}

	public static List<ActorItem> getActorListByIds(List<Integer> ids) {
		MyLog.d(TAG, "getActorListByIds ids : " + ids);
		Context context = VRApplication.getAppContext();
		if (context != null) {
			DbManager db = VRApplication.getDbManager();
			List<ActorItem> actorList = new ArrayList<ActorItem>();
			try {
				for (Integer id : ids) {
					ActorItem aItem = db.selector(ActorItem.class)
							.where(WhereBuilder.b("id", "=", id)).findFirst();
					if (aItem != null) {
						actorList.add(aItem);
					}
				}
				MyLog.d(TAG,
						"getActorListByIds actorList ： " + actorList.toString());
				return actorList;
			} catch (DbException e) {
				MyLog.e(TAG, "getGameListSort failed: " + e);
				return null;
			}
		}
		return null;
	}
	
//	public static final String TITLE_BIRTHDAY = "Birthday";
//	public static final String TITLE_BIRTHPLACE = "Birthplace";
//	public static final String TITLE_LANGUAGES = "Languages";
//	public static final String TITLE_HAIR = "Hair";
//	public static final String TITLE_EYES = "Eyes";
	

	public static String getActorIntroduce(ActorItem actorItem) {
		if(actorItem == null)return null;
		StringBuffer sb = new StringBuffer();
		sb.append(actorItem.getBirthday()+"\n");
		sb.append(actorItem.getArea()+"\n");
		sb.append(actorItem.getLanguage()+"\n");
		sb.append(actorItem.getHair()+"\n");
		sb.append(actorItem.getEyes()+"\n");

		return sb.toString();
	}

	@Override
	public String toString() {
		return "ActorItem [id=" + id + ", area=" + area + ", createtime="
				+ createtime + ", summary=" + summary + ", style=" + style
				+ ", company=" + company + ", age=" + age + ", name=" + name
				+ ", followed=" + followed + ", videoid=" + videoid
				+ ", imgurl=" + imgurl + ", userfollowcount=" + userfollowcount
				+ ", bwh=" + bwh + ", weight=" + weight + ", height=" + height
				+ ", birthday=" + birthday + ", hair=" + hair + ", eyes="
				+ eyes + ", ethnicity=" + ethnicity + ", language=" + language
				+ "]";
	}

	@Override
	public int describeContents() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void writeToParcel(Parcel out, int flags) {

		out.writeInt(id);
		out.writeString(area);
		out.writeString(createtime);
		out.writeString(summary);
		out.writeString(style);
		out.writeString(company);
		out.writeInt(age);
		out.writeString(name);
		out.writeInt(followed);
		out.writeString(videoid);
		out.writeString(imgurl);
		out.writeInt(userfollowcount);
		out.writeString(bwh);
		out.writeString(weight);
		out.writeString(height);
		out.writeString(birthday);
		out.writeString(hair);
		out.writeString(eyes);
		out.writeString(ethnicity);
		out.writeString(language);
	}

	public static final Parcelable.Creator<ActorItem> CREATOR = new Creator<ActorItem>() {

		@Override
		public ActorItem[] newArray(int size) {
			// TODO Auto-generated method stub
			return new ActorItem[size];
		}

		@Override
		public ActorItem createFromParcel(Parcel in) {
			// TODO Auto-generated method stub
			return new ActorItem(in);
		}
	};

}
